<template>
	<view class="uni-numbox">
		<view v-if="inputValue>0"  class="uni-numbox__minus" @click="_calcValue($event,'minus')">
			<image :src="domain+'/TakeOut/static/cefimgs/cart-reduce.png'" class="num-ico"></image>
		</view>
		<view v-if="inputValue>0"  class="uni-numbox__value" type="number" adjust-position="false">
		 <input class="numInput"  type="number" :value="inputValue" disabled="true" @blur="changeCart" @ />
		</view>
		<view class="uni-numbox__plus" @click="_calcValue($event,'plus')">
			<image :src="domain+'/TakeOut/static/cefimgs/cart-no-plus.png'" class="num-ico" v-if="is_limit"></image>
			<image :src="domain+'/TakeOut/static/cefimgs/cart-plus.png'" class="num-ico" v-else></image>
		</view>
	</view>
</template>
<script>
	export default {
		name: 'UniNumberBox',
		props: {
			value: {
				type: [Number, String],
				default: 0
			},
			min: {
				type: Number,
				default: 0
			},
			max: {
				type: Number,
				default: 100
			},
			step: {
				type: Number,
				default: 1
			},
			disabled: {
				type: Boolean,
				default: false
			},
			index:{
				type: [Number, String],
			},
			is_change:{
				type: Boolean,
				default: false
			},
			item:{
				type:[String],
				default: null
			}
		},
		data() {
			return {
				domain:this.resDomain,
				inputValue: 0,
				windowHeight: '',
				modelValue:0,//model
				showHide:false,//显示隐藏
				is_limit:false//限购
			}
		},
		watch: {
			value(val) {
				this.inputValue = +val
			},
			inputValue(newVal, oldVal) {
				if (+newVal !== +oldVal) {
					let a=null
					a={'index':this.index,'num':newVal,'item':this.item}
					if(this.is_change == true){
						this.$emit('change', a)
					}
				}
			}
		},
		created() {
			this.inputValue = +this.value
		},
		methods: {
			_calcValue(e,type) {
				
				
				const scale = this._getDecimalScale()
				let value = this.inputValue * scale
				let step = this.step * scale
				if (type === 'minus') {
					value -= step
				} else if (type === 'plus') {
					value += step
				}
				console.log("this.min"+this.min+"this.max "+ this.max+" value "+value);
				if (value < this.min){
					this.is_limit = false;
					this.inputValue = this.min;
					return
				} 
				if(value > this.max) {
					this.is_limit = true;
					this.inputValue = this.max;
					return 
				}	
				if(value > this.min && value < this.max){
					this.is_limit = false;
				}
				this.inputValue = value / scale;
				
				
				if(this.is_change == false){
					let a=null
					a={'index':this.index,'num':this.inputValue,'item':this.item,'type':type}
					this.$emit('change', a,e.detail)
				}
			},
			_getDecimalScale() {
				let scale = 1
				// 浮点型
				if (~~this.step !== this.step) {
					scale = Math.pow(10, (this.step + '').split('.')[1].length)
				}
				return scale
			},
			changeCart(e){
				if((e.target.value >= this.min && e.target.value <= this.max) || e.target.value==0){
					this.is_limit = false;
					this.inputValue = e.target.value

				}
				if (e.target.value < this.min) {
					this.inputValue = this.min
					this.is_limit = true;

				}
				if (e.target.value > this.max) {
					this.inputValue = this.max
					this.is_limit = true;

				}
				
				if(this.is_change == false){
					let a=null
					a={'index':this.index,'num':this.inputValue,'item':this.item}
					this.$emit('change', a)
				}
			}
		}
	}
</script>
<style>
	@charset "UTF-8";

	.uni-numbox {
		display: inline-flex;
		flex-direction: row;
		justify-content: space-between;
		height: 50rpx;
		position: relative
	}

	.uni-numbox__minus,
	.uni-numbox__plus {
		margin: 0;
		font-size: 40rpx;
		color: #333;
		position: relative;
		width: 50rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
	}

	.uni-numbox__value {
		position: relative;
		/* background-color: #fff; */
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 0 10rpx
	}

	.uni-numbox--disabled {
		color: silver
	}
	
	.numInput{
		width: 50rpx; 
		text-align: center; 
		height: 50rpx; 
		line-height: 50rpx; 
	}
	
	.num-ico{
		margin-top: 5rpx;
		width: 40rpx;
		height: 40rpx;
	}
</style>